package com.jinyu.jyly.ui.mine.account;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.AccountAdapter;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.PropertyListInfo;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账户明细
 */
public class AccountActivity extends BaseActivity<AccountPresenter> implements AccountContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.rcv_recycleview)
    RecyclerView rcvRecycleview;
    @BindView(R.id.cl_empty)
    ConstraintLayout cl_empty;
    @BindView(R.id.tv_balance_yuanbao)
    TextView tv_balance_yuanbao;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private boolean hasLoadMore = false;
    private boolean isFristLoad = true;
    private int PAGE_NO = 1;
    private int PAGE_SIZE = 10;

    private AccountPresenter presenter;
    private AccountAdapter accountAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_account;
    }

    @Override
    protected AccountPresenter getPresenter() {
        if (presenter == null) {
            presenter = new AccountPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        initRecycleView();
        initData();
        float pdb = getIntent().getFloatExtra("pdb", 0.0f);
        float ingot = getIntent().getFloatExtra("ingot", 0.0f);
        tvBalance.setText(pdb + "");
        tv_balance_yuanbao.setText(ingot+"");
    }

    private void initData() {
        try {
            //获取mine
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("page", PAGE_NO); //37
            String param = AES.encryptToHexString(jsonObject.toString());
            presenter.getPropertyList(param);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initRecycleView() {
        rcvRecycleview.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                PAGE_NO = 1;
                try {
                    //获取mine
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("page", PAGE_NO); //37
                    String param = AES.encryptToHexString(jsonObject.toString());
                    presenter.getPropertyList(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                hasLoadMore = false;
            }
        });

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                ++PAGE_NO;
                try {
                    //获取mine
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("page", PAGE_NO); //37
                    String param = AES.encryptToHexString(jsonObject.toString());
                    presenter.getPropertyList(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                hasLoadMore = true;
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.resetNoMoreData();
    }

    /**
     * 获取用户流水
     *
     * @param infoBean
     */
    @Override
    public void onGetPropertyList(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.resetNoMoreData();
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    PropertyListInfo info = gson.fromJson(result, PropertyListInfo.class);
                    if (info != null) {
                        int page = info.getPage();
                        if (isFristLoad) {
                            List<PropertyListInfo.DataBean> beanList = info.getData();
                            accountAdapter = new AccountAdapter(beanList, R.layout.item_account);
                            rcvRecycleview.setAdapter(accountAdapter);
                            isFristLoad = false;
                        } else {
                            if (!hasLoadMore) {
                                //下拉加载
                                List<PropertyListInfo.DataBean> beanList = info.getData();
                                accountAdapter.replaceData(beanList);
                            } else {
                                //上拉加载
                                if (PAGE_NO <= page) {
                                    //说明有值
                                    List<PropertyListInfo.DataBean> beanList = info.getData();
                                    accountAdapter.addData(beanList);
                                }
                            }
                        }
                    }

                    List<PropertyListInfo.DataBean> beanList1 = accountAdapter.getData();
                    if (beanList1 != null && beanList1.size() != 0) {
                        cl_empty.setVisibility(View.GONE);
                        rcvRecycleview.setVisibility(View.VISIBLE);
                    } else {
                        cl_empty.setVisibility(View.VISIBLE);
                        rcvRecycleview.setVisibility(View.GONE);
                    }
//                    List<PropertyListInfo.DataBean> beanList = info.getData();
//                    if (beanList != null && beanList.size() != 0) {
//                        cl_empty.setVisibility(View.GONE);
//                        rcvRecycleview.setVisibility(View.VISIBLE);
//                        AccountAdapter accountAdapter = new AccountAdapter(beanList, R.layout.item_account);
//                        rcvRecycleview.setAdapter(accountAdapter);
//                    }else{
//                        cl_empty.setVisibility(View.VISIBLE);
//                        rcvRecycleview.setVisibility(View.GONE);
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

}
